package com.example.demo.app;

import com.example.demo.ChatMemory.KryoFileChatMemory;
import com.example.demo.advisor.MyCustomAdvisor;
import com.example.demo.advisor.ReReadingAdvisor;
import com.example.demo.model.dto.ai.LoveReport;
import com.example.demo.rag.QueryRewriter;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.api.Advisor;
import org.springframework.ai.chat.client.advisor.vectorstore.QuestionAnswerAdvisor;
import org.springframework.ai.chat.memory.InMemoryChatMemoryRepository;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.MessageType;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CodeApp {

    private final ChatClient chatClient;

    private static final String STORAGE_PATH = "./chat-memory-storage/codeApp";

    private static final String SYSTEM_PROMPT = "你是一位专业的个人学习与创作伙伴(K-Agent)。你的回答总是富有洞察力、精准且结构清晰，旨在激发用户的灵感、辅助思考和提炼想法。";

    private final KryoFileChatMemory chatMemory = new KryoFileChatMemory(STORAGE_PATH);
    @Resource
    private VectorStore PgVectorStore;

    @Resource
    private Advisor codeAppRagCloudAdvisor;

    @Resource
    private QueryRewriter queryRewriter;

    public CodeApp(ChatModel openAiChatModel) {


        chatClient = ChatClient.builder(openAiChatModel)
                .defaultSystem(SYSTEM_PROMPT)
                .defaultAdvisors(
                        MessageChatMemoryAdvisor.builder(new KryoFileChatMemory(STORAGE_PATH)).order(0).build(),
                        new MyCustomAdvisor()
                )
                .build();
    }

    public String doChat(String message, String chatId) {
        String content = chatClient
                .prompt()
                .user(message)
                .advisors(spec -> spec
                        .param("chat_memory_conversation_id", chatId)
                        .param("chat_memory_response_size", 10))
                .call()
                .content();

        //String content = response.getResult().getOutput().getText();
        log.info("response: {}", content);
        return content;
    }



    public String doChatWithRAG(String message, String chatId) {

        List<Message> messages = chatMemory.get(chatId);
        List<String> messagesText = messages.stream()
                .filter(newMessage -> MessageType.USER.equals(newMessage.getMessageType()))
                .map(Message::getText)
                .collect(Collectors.toList());

        String ReMessage = queryRewriter.doQueryRewrite(message);

        String content = chatClient
                .prompt()
                .user(ReMessage)
                .advisors(spec -> spec
                        .param("chat_memory_conversation_id", chatId)
                        .param("chat_memory_response_size", 5))
                //知识库问答
                .advisors(
                        new QuestionAnswerAdvisor(PgVectorStore)
                )
                //检索增强
//                .advisors(codeAppRagCloudAdvisor)
                .call()
                .content();

        //String content = response.getResult().getOutput().getText();
        log.info("response: {}", content);
        return content;
    }

}
